package com.fsx.servertokenlogin.config;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@ConfigurationProperties(value = "config.jwt")
public class JwtConfg {

    long expire;
    String header;
    public long getExpire() {
        return expire;
    }

    public void setExpire(int expire) {
        this.expire = expire;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }
    public String createToken(String uid,String fingerprint){
        Date now=new Date();
        JwtBuilder jwtBuilder= Jwts.builder();
        jwtBuilder.setHeaderParam("typ","JWT")
                .setSubject(uid)
                .setIssuedAt(now)
                .setExpiration(new Date(now.getTime()+expire*60*60*1000))
                .signWith(SignatureAlgorithm.HS512,fingerprint);
        String token=jwtBuilder.compact();
        return token;
    }
    public Claims checkToken(String token,String fingerprint){
        Claims claims;
        claims=Jwts.parser().setSigningKey(fingerprint).parseClaimsJws(token).getBody();
        return claims;
    }
}
